﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Twi.COMMON.WPF
{
    /// <summary>
    /// Interaction logic for ucCodeTextBox.xaml
    /// </summary>
    public partial class CodeTextBox : UserControl
    {
        public CodeTextBox()
        {
            InitializeComponent();
            this.TxtCode_2.TextChanged += new TextChangedEventHandler(TxtCode_2_TextChanged);      
        }

        public new bool Focus()
        {
            return this.TxtCode_2.Focus();
        }

        public bool Validate() 
        {
            if (TxtCode_2.Text.Trim().Length < 2) 
            {
                return false;
            }
            return true;
        }

        void TxtCode_2_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (this.TxtCode_2.Text.Length == 2)
            {
                Text = this.TxtCode_1.Text + this.TxtCode_2.Text;
               
            } 
        }

        private static void OnTextProperyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            CodeTextBox uc = d as CodeTextBox;
            string value = e.NewValue.ToString();
            uc.TxtCode_1.Text = value.SubstringFromStart(2);
            uc.TxtCode_2.Text = value.SubstringFromEnd(2).Trim();//trim添加时会出现空格
        }
        
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(CodeTextBox), new PropertyMetadata("", new PropertyChangedCallback(OnTextProperyChanged)));

        public string Text
        {
            get { return GetValue(TextProperty).ToString(); }
            set
            {
                SetValue(TextProperty, value);
            }
        }
    }
}
